package BLL;

import DAL.connectDatabase;

import java.sql.*;

/**
 *  主页设置
 */
public class homepage {

    static Connection connection=new connectDatabase().connect();
    /**
     * 统计表中的日志个数
     * @return 日志个数
     */
    //添加
    public boolean add(Student student){
        boolean flag=false;

        try {
            PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO student (name,sex,age) VALUES (?,?,?)");

            int index=1;
            preparedStatement.setString(index++,student.getName());
            preparedStatement.setString(index++,student.getSex());
            preparedStatement.setInt(index++,student.getAge());

            flag= preparedStatement.execute();


        } catch (SQLException e) {
            e.printStackTrace();
        }

        return flag;
    }
    //删除
    public boolean delete(int id){
        boolean flag=false;
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM student WHERE id=?");
            preparedStatement.setInt(1,id);
            flag = preparedStatement.execute();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }
    //全部显示
    public ResultSet all() {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM student");
            ResultSet resultSet =null;
            resultSet =preparedStatement.executeQuery();
            return resultSet;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //查找
    public ResultSet singleLog(String name){

        try {

            PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM student WHERE name like \"%\"?\"%\" or sex like \"%\"?\"%\" or age like \"%\"?\"%\"  ");
            preparedStatement.setString(1,name);
            preparedStatement.setString(2,name);
            preparedStatement.setString(3,name);
            ResultSet resultSet=null;
            resultSet = preparedStatement.executeQuery();

            return  resultSet;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public ResultSet singleLog(int id){

        try {
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM student WHERE id=?");

            preparedStatement.setInt(1,id);
            ResultSet resultSet = preparedStatement.executeQuery();

            return  resultSet;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //修改
    public  boolean update(int id,Student student){
        boolean flag=false;

        Connection connect = new connectDatabase().connect();
        try {
            PreparedStatement preparedStatement = connect.prepareStatement("UPDATE student SET name=?,sex=?,age=? WHERE id=?");
            int index=1;
            preparedStatement.setString(index++,student.getName());
            preparedStatement.setString(index++,student.getSex());
            preparedStatement.setInt(index++,student.getAge());
            preparedStatement.setInt(index,id);

            flag = preparedStatement.execute();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  flag;
    }
}
